package com.stephenone.zhdaily.homepage.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.stephenone.zhdaily.Constants;
import com.stephenone.zhdaily.R;
import com.stephenone.zhdaily.data.bean.Story;
import com.stephenone.zhdaily.storydetail.StoryDetailActivity;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by Administrator on 2018/1/29.
 */

public class StoryViewBinder extends ItemViewBinder<Story,StoryViewBinder.ViewHolder> {
    private Context mContext;
    public StoryViewBinder(Context context){
        mContext = context;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_news, parent,
                false);
        return new StoryViewBinder.ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Story item) {
        holder.mTextView.setText(item.getTitle());
        Glide.with(mContext)
                .load(item.getImages().get(0))
                .into(holder.mImageView);
        holder.itemView.setOnClickListener((v) -> {
            Intent intent = new Intent(mContext, StoryDetailActivity.class);
            intent.putExtra(Constants.KEY_STORY_ID,item.getId());
            mContext.startActivity(intent);
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView mTextView;
        public ImageView mImageView;
        public ViewHolder(View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.tv_title);
            mImageView = itemView.findViewById(R.id.iv_image);
        }
    }
}
